unit Chat;

interface

uses
  Database, Color,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, StdCtrls, Buttons, Grids, ExtCtrls, DB, Mask, DBGrids,
  DBCtrls;

type
  TFormChat = class(TForm)
    MainMenu: TMainMenu;
    MenuFile: TMenuItem;
    MenuExit: TMenuItem;
    N2: TMenuItem;
    MenuReturn2: TMenuItem;
    MenuChat: TMenuItem;
    MenuShowAll: TMenuItem;
    N3: TMenuItem;
    MenuClearSelected: TMenuItem;
    MenuClearAll: TMenuItem;
    MenuOptions: TMenuItem;
    MenuStayOnTop: TMenuItem;
    MenuExpanded: TMenuItem;
    MenuHelp: TMenuItem;
    MenuChatHelp: TMenuItem;
    PanelChatBase: TPanel;
    MemoChat: TMemo;
    PopupMenu2: TPopupMenu;
    PopupColorLine2: TMenuItem;
    TimerPause: TTimer;
    PopupAdd: TMenuItem;
    N4: TMenuItem;
    Menu1: TMenuItem;
    Menu2: TMenuItem;
    Menu3: TMenuItem;
    PanelChat: TPanel;
    PanelLines: TPanel;
    MenuShowLineGrid: TMenuItem;
    PanelSeparator: TPanel;
    PanelChatback: TPanel;
    PanelTop: TPanel;
    LabelLine: TLabel;
    PanelHelp: TPanel;
    BtnHelp: TSpeedButton;
    PanelPause: TPanel;
    LEDRed2: TImage;
    LEDRed: TImage;
    LEDGreen: TImage;
    BtnPause: TSpeedButton;
    BtnPlay: TSpeedButton;
    PopupMenu1: TPopupMenu;
    PopupColorLine1: TMenuItem;
    BtnAll: TSpeedButton;
    MenuCtrlTab: TMenuItem;
    MenuCtrlTab2: TMenuItem;
    MenuInvisible: TMenuItem;
    MenuAutotyper: TMenuItem;
    MenuTell: TMenuItem;
    MenuStatus: TMenuItem;
    MenuMessage: TMenuItem;
    MenuImagery: TMenuItem;
    MenuFToolbar: TMenuItem;
    MenuPanic: TMenuItem;
    MenuFiltered: TMenuItem;
    MenuVariables: TMenuItem;
    MenuMap: TMenuItem;
    Data1: TMenuItem;
    Canvas1: TMenuItem;
    MenuSave: TMenuItem;
    N1: TMenuItem;
    DialogSave: TSaveDialog;
    N5: TMenuItem;
    CloseandDisableChatMonitor1: TMenuItem;
    DSChat: TDataSource;
    DSChathist: TDataSource;
    GridLines: TDBGrid;
    GridChathist: TDBGrid;
    EditChat: TDBEdit;
    procedure MenuReturn2Click(Sender: TObject);
    procedure MenuExitClick(Sender: TObject);
    procedure MenuStayOnTopClick(Sender: TObject);
    procedure MenuExpandedClick(Sender: TObject);
    procedure BtnClearOneClick(Sender: TObject);
    procedure BtnHelpClick(Sender: TObject);
    procedure GridChathistDblClick(Sender: TObject);
    procedure GridChathistKeyPress(Sender: TObject; var Key: Char);
    procedure EditChatKeyPress(Sender: TObject; var Key: Char);
    procedure MenuShowAllClick(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DSChathistDataChange(Sender: TObject; Field: TField);
    procedure GridChathistCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure PopupColorLine2Click(Sender: TObject);
    procedure BtnPauseClick(Sender: TObject);
    procedure BtnPlayClick(Sender: TObject);
    procedure TimerPauseTimer(Sender: TObject);
    procedure PopupAddClick(Sender: TObject);
    procedure Menu1Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DSChatDataChange(Sender: TObject; Field: TField);
    procedure MenuShowLineGridClick(Sender: TObject);
    procedure GridLinesKeyPress(Sender: TObject; var Key: Char);
    procedure GridLinesCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure GridLinesDblClick(Sender: TObject);
    procedure PanelSeparatorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure PanelSeparatorMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure PanelSeparatorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PopupColorLine1Click(Sender: TObject);
    procedure MenuCtrlTabClick(Sender: TObject);
    procedure MenuAutotyperClick(Sender: TObject);
    procedure MenuPanicClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure MenuSaveClick(Sender: TObject);
    procedure CloseandDisableChatMonitor1Click(Sender: TObject);
  private
    { Private declarations }
    procedure WMGetMinMaxInfo(var MSG: Tmessage); message WM_GetMinMaxInfo;
    procedure FigureFGBGColor(line: string; var fc: TColor; var bc: TColor);
    procedure ColorLine(line: string);
    procedure UpdateChatbase;
    procedure ShowOneLine(line: string);
  public
    { Public declarations }
    LineGridHeight: integer;
    procedure UpdateLinebase;
    procedure ClosePopup;
    procedure UpdateWidths;
  end;

var
  FormChat: TFormChat;
  mouseisdown, showall, lighted: boolean;

implementation

uses Main, DBClient;

{$R *.DFM}

procedure TFormChat.FormCreate(Sender: TObject);
begin
  LineGridHeight:=66;
  showall:=true;
  lighted:=false;
  UpdateChatbase;
  UpdateLinebase;
end;

procedure TFormChat.WMGetMinMaxInfo(var MSG: Tmessage);
begin
  inherited;
  with PMinMaxInfo(MSG.lparam)^ do begin
    with ptMinTrackSize do begin
      X := 200;
      Y := LineGridHeight+167
    end;
    with ptMaxTrackSize do begin
      X := 9999;
      Y := 9999;
    end;
  end;
end;

procedure TFormChat.MenuReturn2Click(Sender: TObject);
begin
  if BtnPlay.enabled then BtnPlay.click;
  FormMain.show;
end;

procedure TFormChat.MenuExitClick(Sender: TObject);
begin
  close;
end;

procedure TFormChat.MenuStayOnTopClick(Sender: TObject);
begin
  MenuStayOnTop.checked:=not MenuStayOnTop.checked;
  if MenuStayOnTop.checked = true then formstyle:=fsstayontop
  else formstyle:=fsnormal;
end;

procedure TFormChat.MenuExpandedClick(Sender: TObject);
begin
  MenuExpanded.checked:=not MenuExpanded.checked;
  UpdateChatbase;
end;

const
   ONE_LINE = '(Master = %s) and (LINE = "%s")';

procedure TFormChat.BtnClearOneClick(Sender: TObject);
begin
//  0=All Chats, 1=One Line
  with Datawell.TableChathist do begin
    if (sender as TComponent).tag = 1 then
      filter := format(ONE_LINE, [Datawell.masterindex, Datawell.TableChat.fieldbyname('Line').asstring])
    else
      filter := 'Master = '+Datawell.masterindex;
    filtered := true;
    active:=true;
    first;
    while not EOF do delete;
  end;   
  if (sender as TComponent).tag = 1 then begin
    if not Datawell.TableChat.EOF then Datawell.TableChat.delete;
    showall:=false;
  end
  else begin
    with Datawell.TableChat do begin
      first;
      while not EOF do delete;
    end;
  end;
  MenuShowAll.click;
end;

procedure TFormChat.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('chat_monitor');
end;

procedure TFormChat.GridChathistDblClick(Sender: TObject);
begin
  if not showall then begin
    MenuShowAll.click;
    exit;
  end;
  showall:=false;
  ShowOneLine(Datawell.TableChathist.fieldbyname('Line').asstring);
  with Datawell.TableChat do begin
    first;
    while not EOF do begin
      if Datawell.TableChat.fieldbyname('Line').asstring =
        Datawell.TableChathist.fieldbyname('Line').asstring then break;
      next;
    end;
  end;
  EditChat.setfocus;
end;

procedure TFormChat.ShowOneLine(line: string);
var
  fcol, bcol: TColor;
begin
  if line = '' then exit;
  with Datawell.TableChathist do begin
    active:=false;
    filter := format(ONE_LINE, [Datawell.masterindex, line]);
    filtered := true;
    active:=true;
    last;
  end;
  LabelLine.caption:=line+' line';
  FigureFGBGColor(Datawell.TableChathist.fieldbyname('Line').asstring,fcol,bcol);
  LabelLine.font.color:=fcol;
  LabelLine.color:=bcol;
  PanelTop.color:=LabelLine.color;
  PanelHelp.color:=LabelLine.color;
  UpdateChatbase;
  GridChathist.repaint;
  BtnAll.enabled:=true;
  UpdateWidths;
end;

procedure TFormChat.GridChathistKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridChathistDblClick(Sender);
  end;
end;

procedure TFormChat.EditChatKeyPress(Sender: TObject; var Key: Char);
var work: string;
begin
  if key = #13 then begin
    key:=#0;
    if not Datawell.connected then begin
      messagedlg('Not connected to server',mtinformation,[mbok],0);
      exit;
    end;
    work:=EditChat.text;
    if work = '' then exit;
    if copy(work,1,1) = Datawell.TableOptions.fieldbyname('Activator').asstring then begin
      delete(work,1,1);
      FormMain.SendData(work,1);
    end
    else begin
      if Datawell.TableChat.fieldbyname('Line').asstring = '' then exit;
      FormMain.SendData(Datawell.TableChathist.fieldbyname('Command').asstring+' '+work,1);
    end;
    EditChat.text:='';
  end;
end;

procedure TFormChat.MenuShowAllClick(Sender: TObject);
begin
  with Datawell.TableChathist do begin
    active:=false;
    filter := 'Master = '+Datawell.masterindex;
    filtered := true;
    active:=true;
    last;
  end;
  showall:=true;
  LabelLine.caption:='';
  LabelLine.font.color:=clblack;
  LabelLine.color:=clsilver;
  PanelTop.color:=clsilver;
  PanelHelp.color:=clsilver;
  UpdateChatbase;
  GridLines.repaint;
  BtnAll.enabled:=false;
  UpdateWidths;
end;

procedure TFormChat.FormResize(Sender: TObject);
begin
  EditChat.width:=width-8;
  UpdateWidths;
end;

procedure TFormChat.DSChathistDataChange(Sender: TObject; Field: TField);
begin
  MemoChat.text:=Datawell.TableChathist.fieldbyname('Message').asstring;
  if Datawell.TableChathist.fieldbyname('Line').asstring = '' then begin
    PopupColorLine2.caption:='&Color chat line';
    PopupColorLine2.enabled:=false;
    PopupAdd.caption:='&Add person to the Tell Monitor';
    PopupAdd.enabled:=false;
    exit;
  end;
  PopupColorLine2.caption:='&Color the '+Datawell.TableChathist.fieldbyname('Line').asstring+' line';
  PopupColorLine2.enabled:=true;
  PopupAdd.caption:='&Add '+Datawell.TableChathist.fieldbyname('From').asstring+' to the Tell Monitor';
  PopupAdd.enabled:=true;
end;

procedure TFormChat.GridChathistCalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
var fcol, bcol: TColor;
begin
  if Field.fieldname = 'Message' then begin
    FigureFGBGColor(Datawell.TableChathist.fieldbyname('Line').asstring,fcol,bcol);
    AFont.color:=fcol;
    ABrush.color:=bcol;
  end
end;

procedure TFormChat.GridLinesCalcCellColors(Sender: TObject; Field: TField;
  State: TGridDrawState; highlight: Boolean; AFont: TFont; ABrush: TBrush);
var fcol, bcol: TColor;
begin
  if Field.fieldname = 'Line' then begin
    FigureFGBGColor(Datawell.TableChat.fieldbyname('Line').asstring,fcol,bcol);
    AFont.color:=fcol;
    ABrush.color:=bcol;
  end
end;

procedure TFormChat.FigureFGBGColor(line: string; var fc: TColor; var bc: TColor);
begin
  with Datawell.TableChatlines do begin
    filter := format(ONE_LINE, [Datawell.masterindex, line]);
    filtered := true;
    active:=true;
    if Datawell.TableChatlines.fieldbyname('Line').asstring = '' then begin
      fc:=clblack;
      bc:=clsilver;
    end
    else begin
      fc:=Datawell.TableChatlines.fieldbyname('FGColor').asinteger;
      bc:=Datawell.TableChatlines.fieldbyname('BGColor').asinteger;
    end;
    filter := 'Master = '+Datawell.masterindex;
    filtered := true;
    active:=true;
  end;
end;

procedure TFormChat.PopupColorLine2Click(Sender: TObject);
begin
  ColorLine(Datawell.TableChathist.fieldbyname('Line').asstring);
end;

procedure TFormChat.ColorLine(line: string);
var
  form: TFormColor;
  fi, bi: integer;
  didfind: boolean;
  fcol, bcol: TColor;
begin
  didfind:=false;
  with Datawell.TableChatlines do begin
    first;
    while not EOF do begin
      if Datawell.TableChatlines.fieldbyname('Line').asstring = line then begin
        didfind:=true;
        break;
      end;
      next;
    end;
  end;
  form:=TFormColor.create(self);
  if not didfind then begin
    form.ColorGrid.foregroundindex:=0;
    form.ColorGrid.backgroundindex:=7;
  end
  else begin
    form.ColorGrid.foregroundindex:=
      Datawell.ColorFigure(Datawell.TableChatlines.fieldbyname('FGColor').asinteger);
    form.ColorGrid.backgroundindex:=
      Datawell.ColorFigure(Datawell.TableChatlines.fieldbyname('BGColor').asinteger);
  end;
  form.showmodal;
  fi:=form.findex;
  bi:=form.bindex;
  form.free;
  if (fi < 0) or (bi < 0) then exit;
  if didfind then begin
    with Datawell.TableChatlines do begin
      first;
      while not EOF do begin
        if Datawell.TableChatlines.fieldbyname('Line').asstring = line then break;
        next;
      end;
    end;
    Datawell.TableChatlines.edit;
  end
  else begin
    Datawell.TableChatlines.append;
    Datawell.TableChatlines.fieldbyname('Master').asinteger:=Datawell.masterinteger;
    Datawell.TableChatlines.fieldbyname('Line').asstring:=line;
  end;
  Datawell.TableChatlines.fieldbyname('FGColor').asinteger:=Datawell.FigureColor(fi);
  Datawell.TableChatlines.fieldbyname('BGColor').asinteger:=Datawell.FigureColor(bi);
  Datawell.TableChatlines.post;
  GridLines.repaint;
  GridChathist.repaint;
  if showall then exit;
  FigureFGBGColor(Datawell.TableChathist.fieldbyname('Line').asstring,fcol,bcol);
  LabelLine.font.color:=fcol;
  LabelLine.color:=bcol;
  PanelTop.color:=LabelLine.color;
  PanelHelp.color:=LabelLine.color;
end;

procedure TFormChat.BtnPauseClick(Sender: TObject);
begin
  lighted:=true;
  LEDRed.bringtofront;
  TimerPause.enabled:=true;
  BtnPause.enabled:=false;
  BtnPlay.enabled:=true;
  Datawell.PauseMessages(0,true);
end;

procedure TFormChat.BtnPlayClick(Sender: TObject);
begin
  LEDGreen.bringtofront;
  TimerPause.enabled:=false;
  BtnPause.enabled:=true;
  BtnPlay.enabled:=false;
  Datawell.PauseMessages(0,false);
end;

procedure TFormChat.TimerPauseTimer(Sender: TObject);
begin
  lighted:=not lighted;
  if lighted then LEDRed.bringtofront else LEDRed2.bringtofront;
end;

procedure TFormChat.PopupAddClick(Sender: TObject);
var didfind: boolean;
begin
  didfind:=false;
  with Datawell.TableTell do begin
    first;
    while not EOF do begin
      if Datawell.TableTell.fieldbyname('From').asstring =
          Datawell.TableChathist.fieldbyname('From').asstring then begin
        didfind:=true;
        break;
      end;
      next;
    end;
    if not didfind then begin
      append;
      Datawell.TableTell.fieldbyname('From').asstring:=
        Datawell.TableChathist.fieldbyname('From').asstring;
      Datawell.TableTell.fieldbyname('Amt').asinteger:=0;
      post;
    end;
  end;
  FormMain.MenuShowTell.click;
end;

procedure TFormChat.Menu1Click(Sender: TObject);
begin
  TMenuItem(sender).checked:=not TMenuItem(sender).checked;
  Menu1.checked:=false;
  Menu2.checked:=false;
  Menu3.checked:=false;
  case TMenuItem(sender).tag of
    1: Menu1.checked:=true;
    2: Menu2.checked:=true;
    3: Menu3.checked:=true;
  end;
//  GridChathist.rowheightpercent:=90*TMenuItem(sender).tag;
end;

procedure TFormChat.UpdateChatbase;
var heightCB, heightC: integer;
begin
  heightCB:=0;
  heightC:=0;
  if not showall then begin
    inc(heightC,22);
    inc(heightCB,22);
    EditChat.tabstop:=true;
  end
  else EditChat.tabstop:=false;
  if MenuExpanded.checked then inc(heightCB,51);
  PanelChatbase.height:=heightCB;
  PanelChat.height:=heightC;
end;

procedure TFormChat.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_PAUSE then begin
    if BtnPause.enabled then begin
      BtnPause.click;
      if visible then GridChathist.setfocus;
    end
    else begin
      BtnPlay.click;
      if not showall then begin
        if visible then EditChat.setfocus;
      end;
    end;
    exit;
  end
  else if key = VK_PRIOR then begin
    if activecontrol = GridChathist then begin
      if BtnPause.enabled then BtnPause.click;
    end;
  end;
end;

procedure TFormChat.DSChatDataChange(Sender: TObject; Field: TField);
begin
  if Datawell.TableChat.fieldbyname('Line').asstring = '' then begin
    PopupColorLine1.caption:='&Color chat line';
    PopupColorLine1.enabled:=false;
    MenuClearSelected.caption:='&Clear Chats from Line';
    MenuClearSelected.enabled:=false;
    exit;
  end;
  PopupColorLine1.caption:='&Color the '+Datawell.TableChat.fieldbyname('Line').asstring+' line';
  PopupColorLine1.enabled:=true;
  MenuClearSelected.caption:='&Clear Chats from the '+
    Datawell.TableChat.fieldbyname('Line').asstring+' line';
  MenuClearSelected.enabled:=true;
end;

procedure TFormChat.MenuShowLineGridClick(Sender: TObject);
begin
  MenuShowLineGrid.checked:=not MenuShowLineGrid.checked;
  UpdateLinebase;
end;

procedure TFormChat.UpdateLinebase;
var heightp: integer;
begin
  heightp:=0;
  if MenuShowLineGrid.checked then inc(heightp,LineGridHeight);
  PanelLines.height:=heightp;
  if heightp > 0 then GridLines.tabstop:=true else GridLines.tabstop:=false;
end;

procedure TFormChat.GridLinesKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridLinesDblClick(Sender);
  end;
end;

procedure TFormChat.GridLinesDblClick(Sender: TObject);
begin
  showall:=false;
  ShowOneLine(Datawell.TableChat.fieldbyname('Line').asstring);
  GridLines.repaint;
end;

procedure TFormChat.PanelSeparatorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  mouseisdown:=true;
end;

procedure TFormChat.PanelSeparatorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  mouseisdown:=false;
end;

procedure TFormChat.PanelSeparatorMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var newheight: integer;
begin
  if not mouseisdown then exit;
  newheight:=PanelLines.height+y;
  if newheight > (height-173) then newheight:=height-173;
  if newheight < 47 then newheight:=47;
  PanelLines.height:=newheight;
  LineGridHeight:=newheight;
end;

procedure TFormChat.PopupColorLine1Click(Sender: TObject);
begin
  ColorLine(Datawell.TableChat.fieldbyname('Line').asstring);
end;

procedure TFormChat.UpdateWidths;
begin
  Datawell.TableChathist.fieldbyname('Message').displaywidth:=((width - 99) div 7)+4;
end;

procedure TFormChat.ClosePopup;
var temp: TWinControl;
begin
  if not active then exit;
  temp:=activecontrol;
  PostMessage(handle,WM_LBUTTONDOWN,MK_LBUTTON,1);
  PostMessage(handle,WM_LBUTTONUP,MK_LBUTTON,1);
  activecontrol:=temp;
end;

procedure TFormChat.MenuCtrlTabClick(Sender: TObject);
begin
  if TMenuItem(sender).tag = 0 then FormMain.NextScreen(2)
  else FormMain.PriorScreen(2);
end;

procedure TFormChat.MenuAutotyperClick(Sender: TObject);
begin
  FormMain.OpenFloater(TComponent(sender).tag);
end;

procedure TFormChat.MenuPanicClick(Sender: TObject);
begin
  FormMain.MenuPanic.click;
end;

procedure TFormChat.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Datawell.chatspaused then Datawell.PauseMessages(0,false);
  FormMain.UnloadScreen(0);
end;

procedure TFormChat.MenuSaveClick(Sender: TObject);
var
  F: TextFile;
  work: string;
  table: TClientDataset;
begin
  DialogSave.initialdir:=Datawell.GetDir(4);
  if not DialogSave.execute then exit;
  Datawell.SetDir(4,DialogSave.filename);
  AssignFile(F,DialogSave.filename);
  Rewrite(F);
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableChatHist, false);
    filtered := true;
    active:=true;
    first;
    work:='All Chatlines';
    if PanelTop.color <> clsilver then
      work:='Chatline: '+fieldbyname('Line').asstring;
    WriteLn(F,work+'  ['+timetostr(time)+'  '+datetostr(date)+']');
    WriteLn(F,'--------');
    while not EOF do begin
      WriteLn(F,fieldbyname('Line').asstring+': '+fieldbyname('Message').asstring);
      next;
    end;
  end;
  table.free;
  CloseFile(F);
end;

procedure TFormChat.CloseandDisableChatMonitor1Click(Sender: TObject);
begin
  with Datawell.TableOptions3 do begin
    edit;
    fieldbyname('L1').asboolean:=false;
    post;
  end;
  close;
end;

end.
